﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Text;
using Gatheshop.Common;
using Gatheshop.Web.Plugin.Feedback.BLL;
using Gatheshop.Web.Plugin.Feedback.Model;

namespace Gatheshop.Web.Plugin.Feedback
{
    public class ajax : IHttpHandler, IRequiresSessionState
    {
        // Methods
        private void feedback_add(HttpContext context)
        {
            StringBuilder builder = new StringBuilder();
            feedbackBLL feedback = new feedbackBLL();
            feedback model = new feedback();
            string formString = GESRequest.GetFormString("txtCode");
            string str2 = GESRequest.GetFormString("txtTitle");
            string str3 = GESRequest.GetFormString("txtContent");
            string str4 = GESRequest.GetFormString("txtUserName");
            string str5 = GESRequest.GetFormString("txtUserTel");
            string str6 = GESRequest.GetFormString("txtUserQQ");
            string str7 = GESRequest.GetFormString("txtUserEmail");
            if (string.IsNullOrEmpty(formString))
            {
                context.Response.Write("{msg:0, msgbox:\"对不起，请输入验证码！\"}");
            }
            else if (context.Session["ges_session_code"] == null)
            {
                context.Response.Write("{msg:0, msgbox:\"对不起，系统找不到生成的验证码！\"}");
            }
            else if (formString.ToLower() != context.Session["ges_session_code"].ToString().ToLower())
            {
                context.Response.Write("{msg:0, msgbox:\"您输入的验证码与系统的不一致！\"}");
            }
            else if (string.IsNullOrEmpty(str3))
            {
                context.Response.Write("{msg: 0, msgbox: \"对不起，请输入留言的内容！\"}");
            }
            else
            {
                model.title = str2;
                model.content = Utils.ToHtml(str3);
                model.user_name = str4;
                model.user_tel = str5;
                model.user_qq = str6;
                model.user_email = str7;
                model.add_time = DateTime.Now;
                model.is_lock = 1;
                if (feedback.Add(model) > 0)
                {
                    context.Response.Write("{msg: 1, msgbox: \"恭喜您，留言提交成功啦！\"}");
                }
                else
                {
                    context.Response.Write("{msg: 0, msgbox: \"对不起，保存过程中发生错误！\"}");
                }
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            string queryString = GESRequest.GetQueryString("action");
            if ((queryString != null) && (queryString == "add"))
            {
                this.feedback_add(context);
            }
        }

        // Properties
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }


}